﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using Microsoft.VisualBasic;
using System.Diagnostics;
using System.IO;

namespace app_common
{
    /// <summary>
    /// Auth: 
    /// Date: October  2010
    /// Desc: 
    /// </summary>
    public static class ENDECODE
    {

        public static string getMD5(string input)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        /// <summary>
        /// Auth: 
        /// Date: October  2010
        /// Desc: Encrypt toDecrypt string base on key string
        /// </summary>
        /// <param name="toDecrypt"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Decrypt(string toDecrypt)
        {
            string key = "i_have_a_new_key";
            try
            {
                byte[] inputBuffer = Convert.FromBase64String(toDecrypt.Replace(";;;;;", "="));
                byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(key));
                TripleDESCryptoServiceProvider provider2 = new TripleDESCryptoServiceProvider();
                provider2.Key = buffer;
                provider2.Mode = CipherMode.ECB;
                provider2.Padding = PaddingMode.PKCS7;
                byte[] bytes = provider2.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
                return Encoding.UTF8.GetString(bytes);
            }
            catch (Exception)
            {
                return "";
            }
        }

        /// <summary>
        /// Auth: 
        /// Date: October  2010
        /// Desc: Decrypt toEncrypt string base on key string
        /// </summary>
        /// <param name="toEncrypt"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Encrypt(string toEncrypt)
        {
            string key = "i_have_a_new_key";
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(toEncrypt);
                byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(key));
                TripleDESCryptoServiceProvider provider2 = new TripleDESCryptoServiceProvider();
                provider2.Key = buffer;
                provider2.Mode = CipherMode.ECB;
                provider2.Padding = PaddingMode.PKCS7;
                byte[] inArray = provider2.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);
                return Convert.ToBase64String(inArray, 0, inArray.Length).Replace("=", ";;;;;");
            }
            catch (Exception)
            {
                return "";
            }
        }

        static public string Dencrypt(string stData)
        {
            byte[] IV = new byte[] { 0x67, 0x83, 0x19, 0x24, 0x2f, 0xa8, 0xa9, 2, 3, 0x5e, 0x1c, 0x6d, 0x16, 9, 40, 90 };
            byte[] key = new byte[] { 0x65, 0x83, 0xca, 230, 4, 0, 0x7d, 2, 0x81, 0x2d, 0x13, 0x13, 0x12, 3, 4, 100, 0x80, 2, 0x1f, 4, 50, 1, 2, 3, 4, 0, 1, 2, 3, 4, 200, 100 };

            int num;
            ASCIIEncoding encoding = new ASCIIEncoding();
            RijndaelManaged managed = new RijndaelManaged();
            byte[] buffer = new byte[((int)Math.Round((double)((((double)stData.Length) / 2.0) - 1.0))) + 1];
            string str3 = stData;
            int num5 = buffer.Length - 1;
            for (num = 0; num <= num5; num++)
            {
                buffer[num] = Microsoft.VisualBasic.CompilerServices.ByteType.FromString("&H" + str3.Substring(0, 2));
                str3 = str3.Substring(2, str3.Length - 2);
            }
            ICryptoTransform transform = managed.CreateDecryptor(key, IV);
            MemoryStream stream2 = new MemoryStream(buffer);
            CryptoStream stream = new CryptoStream(stream2, transform, CryptoStreamMode.Read);
            byte[] buffer2 = new byte[buffer.Length + 1];
            stream.Read(buffer2, 0, buffer2.Length);
            int num2 = 0;
            int num4 = buffer2.Length - 1;
            for (num = 0; num <= num4; num++)
            {
                if (buffer2[num] != 0)
                {
                    num2++;
                }
            }
            byte[] bytes = new byte[(num2 - 1) + 1];
            int num3 = num2 - 1;
            for (num = 0; num <= num3; num++)
            {
                bytes[num] = buffer2[num];
            }
            return encoding.GetString(bytes);
        }
    }
}
